package cn.hnc.checkapi.repository;

import java.util.List;

import cn.hnc.checkapi.bean.ConfigType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

public interface ConfigTypeRepository extends JpaRepository<ConfigType, Long> {
   Page findAll(Pageable pageable);

   @Query(
      value = "select value from ConfigType c where   id =153 ",
      nativeQuery = true
   )
   String getChecksum();

   @Query(
      value = "select * from ConfigType c where   if(ISNULL(?1) || length(ltrim(rtrim(?1)))<1,1=1, c.configType like %?1%) and if(ISNULL(?2) || length(trim(?2)) <1,1=1,c.value like %?2%)",
      nativeQuery = true
   )
   List findConfigTypeBy(String configType, String value);

   @Query(
      value = "select * from ConfigType c where   if(ISNULL(?1) || length(ltrim(rtrim(?1)))<1,1=1, c.configType =?1) and if(ISNULL(?2) || length(trim(?2)) <1,1=1,c.value =?2)",
      nativeQuery = true
   )
   List findConfigTypeById(String configType, String value);

   @Modifying
   @Query(
      value = "update ConfigType set configType=?2,value =?3 where id =?1",
      nativeQuery = true
   )
   int updateConfigType(Long id, String configType, String value);

   @Modifying
   @Query(
      value = "update ConfigType set value =?1 where id =153",
      nativeQuery = true
   )
   int updateChecksum(String value);

   @Modifying
   @Query(
      value = "delete from ConfigType where id =?1",
      nativeQuery = true
   )
   int delConfigType(Long id);
}
